<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/5 0005
 * Time: 下午 3:00
 */
namespace app\admin\controller;

class Cashregister extends Base{   //store_member

    public function _initialize()
    {
        parent::_initialize();
        $this->model = 'store_member';
//        $defaultWhere = $this->getDefaultWhere();
//        $this->defaultWhere = $defaultWhere;
//        $authMdl = model('role');
//        $authRole = $authMdl->where('status',1)->select();
//        $this->assign('authRole',$authRole);
    }

    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        // Reset filter
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = db($this->model);

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'] + 1;
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }
            $join = [   //store_auth_group_access   store_auth_group
                ['__STORE_AUTH_GROUP_ACCESS__ b','b.uid=a.id'],
                ['__STORE_AUTH_GROUP__ c','c.id=b.group_id'],
                ['__STORE__ d','d.id=a.store_id']
            ];
            $fields = 'a.id,a.name,a.cashier_account,a.lastlogin_time,c.title,d.name as sname';
            $data = $model->alias('a')
                ->where($where)
                ->join($join)
                ->field($fields)
                ->limit($start, $length)
                ->order($order)
                ->select();

            $count = $model->where($where)->count();
            foreach($data as $k => $v)
            {
                $v['sort'] = $k + 1;
                if ($v['type'] === 0){
                    $v['role_name'] = '超级管理员';
                    $v['name'] = '超级管理员';
                }
            }
            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return $this->fetch('list');

    }
}